/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.servlet.niceshot;

import br.com.login.niceshot.LoginQueries;
import br.com.niceshot.PresetQueries;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alexandre
 */
@SuppressWarnings("serial")
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    LoginQueries pQueries = new LoginQueries();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        String logoff = request.getParameter("logoff");

        if (!logoff.isEmpty()) {
            request.getSession().setAttribute("Usuario", null);
            request.getSession().setAttribute("Admin", null);
            response.sendRedirect("index.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        response.setContentType("text/html;charset=UTF-8");

        try (PrintWriter out = response.getWriter()) {

            if (request.getSession().getAttribute("Usuario") != null) {
                String senhaAtual = request.getParameter("cd_senha_atual");
                String senhaNova = request.getParameter("cd_senha");

                out.println(pQueries.updateSenha(String.valueOf(request.getSession().getAttribute("Usuario")), senhaAtual, senhaNova));
            } else {
                String user = request.getParameter("usuario");
                String senha = request.getParameter("senha");
                String respostaAuth = pQueries.SelectLogin(user, senha);

                if (respostaAuth.equals("A")) {
                    request.getSession().setAttribute("Usuario", user);
                    request.getSession().setAttribute("Admin", user);
                } else if (respostaAuth.equals("C")) {
                    request.getSession().setAttribute("Usuario", user);
                } else if (respostaAuth.equals("I")) {
                    out.println("<div id=\"iconModal\"><span class=\"glyphicon glyphicon-ban-circle\"></span></div><div class=\"row\"><h3 align=\"center\">Conta desativada.</h3></div>");
                } else {
                    out.println("<div id=\"iconModal\"><span class=\"glyphicon glyphicon-ban-circle\"></span></div><div class=\"row\"><h3 align=\"center\">Usuário e/ou senha incorreto(s)!</h3></div>");
                }
            }
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
